
package mytomcat.util;

import mytomcat.dto.XmlNodeInfo;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.File;
import java.util.*;

/**
 * <ul>
 * <li>Title: Dom4j解析xml的工具类</li>
 * <li>Description: TODO </li>
 * <li>Copyright: Copyright (c) 2018</li>
 * <li>Company: /</li>
 * </ul>
 *
 * @author swc
 * @version V1.0
 * @date 2020/5/29 0029 上午 10:37
 */
public class Dom4jParseUtil {

    public static XmlNodeInfo xmlParseContext(String filePath) {
        Map<String,XmlNodeInfo> hashMap =new HashMap<String, XmlNodeInfo>();

        XmlNodeInfo xmlNodeInfo =new XmlNodeInfo();
        SAXReader reader = new SAXReader();
        try {
            String id ="";

            // 通过reader对象的read方法加载books.xml文件,获取docuemnt对象。
            Document document = reader.read(new File(filePath));
            // 通过document对象获取根节点bookstore
            Element firstEle = document.getRootElement();
            xmlNodeInfo.setName(firstEle.getName());
            xmlNodeInfo.setParentName("");//父节点
            xmlNodeInfo.setValue(firstEle.getText());
            xmlNodeInfo.setAttributes(firstEle.attributes());

            for(int i=0;i<firstEle.attributes().size();i++){
                Attribute attribute =(Attribute)firstEle.attributes().get(i);
                if("id".equals(attribute.getName())){
                    id =attribute.getValue();
                }
            }
            xmlNodeInfo.setUuidKey(firstEle.getName());
            xmlNodeInfo.setId(id);

            hashMap.put(xmlNodeInfo.getUuidKey(),xmlNodeInfo);

            getList(firstEle,xmlNodeInfo,hashMap);
        } catch (DocumentException e) {
            e.printStackTrace();
        }

        xmlNodeInfo.setAllNode(hashMap);
        return xmlNodeInfo;
    }

    private static void getList(Element firstEle, XmlNodeInfo xmlNodeInfo, Map<String, XmlNodeInfo> hashMap) {
        Iterator it = firstEle.elementIterator();
        List<XmlNodeInfo> list =null;
        while (it.hasNext()) {
            Element elment = (Element) it.next();

            XmlNodeInfo child  =new XmlNodeInfo();

            if(xmlNodeInfo.getXmlNodeInfoList()!=null && xmlNodeInfo.getXmlNodeInfoList().size()>0){
                list =xmlNodeInfo.getXmlNodeInfoList();
            }else {
                list =new ArrayList<XmlNodeInfo>();
            }
            //节点属性值
            List attributes = elment.attributes();

            String id="";
            for(int i=0;i<attributes.size();i++){
                Attribute attribute =(Attribute)attributes.get(i);
                if("id".equals(attribute.getName())){
                    id =attribute.getValue();
                }
            }

            child.setName(elment.getName());
            child.setParentName(xmlNodeInfo.getName());//父节点
            child.setValue(elment.getText());
            child.setAttributes(attributes);
            child.setId(id);
            child.setUuidKey(child.getParentName()+elment.getName());
            list.add(child);

            xmlNodeInfo.setXmlNodeInfoList(list);

            int ind =0;
            if(hashMap.containsKey(child.getUuidKey()+ind)){
                ind++;
                hashMap.put(child.getUuidKey()+ind,child);
            }else{
                ind =0;
                hashMap.put(child.getUuidKey()+ind,child);
            }



            getList(elment, child, hashMap);
        }
    }




}
